package com.mask.roll.common.exception;

import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.code.SystemEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 * @author hx
 * @since 2021/10/21 3:31 下午
 */

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public RollResponse error(Exception e){
        log.error("系统异常:{}",e.getMessage());
        e.printStackTrace();
        return RollResponse.result(SystemEnums.SYSTEM_ERROR.getCode(),
                RollConstants.ERROR,SystemEnums.SYSTEM_ERROR.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public RollResponse arithmetic(ArithmeticException e){
        log.error("算术异常:{}",e.getMessage());
        return RollResponse.result(SystemEnums.SYSTEM_ARITHMETIC.getCode(),
                RollConstants.ERROR,SystemEnums.SYSTEM_ARITHMETIC.getMessage(),e.getMessage());
    }

    @ExceptionHandler(RollException.class)
    @ResponseBody
    public RollResponse rollException(RollException e){
        return RollResponse.result(e.getCode(),e.getLevel(), e.getMessage());
    }

    @ExceptionHandler(FileUploadBase.FileSizeLimitExceededException.class)
    @ResponseBody
    public RollResponse fileUploadError(FileUploadBase.FileSizeLimitExceededException e) {
        log.error("上传文件异常:{}",e.getMessage());
        return RollResponse.result(SystemEnums.FILE_UPLOAD_ERROR.getCode(),
                RollConstants.ERROR,SystemEnums.FILE_UPLOAD_ERROR.getMessage());
    }

}
